local skill = fk.CreateSkill {
  name = "lvshi__tianfu",
  tags = {"Turn"}
}

Fk:loadTranslationTable{
  ["lvshi__tianfu"] = "天覆",
  [":lvshi__tianfu"] = "你可将一张红色/黑色牌当【过河拆桥】/【无懈可击】使用；若中央区内黑色/红色牌数量较多，此【过河拆桥】额外结算X次/"..
  "你摸X张牌，然后此回合不能再发动“天覆”。（X为中央区内红色牌和黑色牌的差）",
  ["#lvshi__tianfu-prompt"] = "你可将红色牌当【过河拆桥】使用，黑色牌当【无懈可击】使用",

  ["$lvshi__tianfu1"] = "效逐日之夸父，怀忠志而长存。",
  ["$lvshi__tianfu2"] = "知天命而不顺，履穷途而强为。",
}

local DIY = require "packages/diy_utility/diy_utility"
local U = require "packages/utility/utility"

skill:addAcquireEffect(function (self, player, is_start)
  player.room:addSkill("#CenterArea")
end)

skill:addEffect("viewas", {
  prompt = "#lvshi__tianfu-prompt",
  pattern = "dismantlement,nullification",
  interaction = function(self, player)
    local all_names = {"dismantlement", "nullification"}
    local invoke_names = table.filter(Fk:getAllCardNames("t"), function (name)
      return table.contains(all_names, Fk:cloneCard(name).trueName)
    end)
    return UI.CardNameBox { choices = invoke_names }
  end,
  card_filter = function(self, player, to_select, selected)
    if Fk.currentResponsePattern == "nullification" then
      return Fk:getCardById(to_select).color == Card.Black
    elseif Fk.currentResponsePattern == nil then
      return Fk:getCardById(to_select).color == Card.Red
    end
  end,
  view_as = function(self, player, cards)
    if #cards ~= 1 or not self.interaction.data then return end
    local card = Fk:cloneCard(self.interaction.data)
    card:addSubcards(cards)
    card.skillName = skill.name
    return card
  end,
  enabled_at_response = function(self, player, response)--FIXME：pattern解析目前有问题
    return not response
  end,
  before_use = function (self, player, use)
    local room = player.room
    local ids = DIY.getCenterCards(room)
    local reds = #table.filter(ids, function(id) return Fk:getCardById(id).color == Card.Red end)
    local blacks = #table.filter(ids, function(id) return Fk:getCardById(id).color == Card.Black end)
    local n = math.abs(reds - blacks)
    if use.card.trueName == "nullification" and reds > blacks then
      player:drawCards(n, skill.name)
      room:invalidateSkill(player, skill.name, "-turn")
    elseif use.card.trueName == "dismantlement" and blacks > reds then
      use.additionalEffect = (use.additionalEffect or 0) + n
      room:invalidateSkill(player, skill.name, "-turn")
    end
  end
})

return skill

